/**
 * Project Name:MVC
 * File Name:TestJsonController.java
 * Package Name:cn.com.tupperware.controller
 * Date:Nov 8, 20169:59:12 AM
 * Copyright (c) 2016, chenzhou1025@126.com All Rights Reserved.
 *
*/

package cn.com.tupperware.controller;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * ClassName:TestJsonController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: Nov 8, 2016 9:59:12 AM <br/>
 * 
 * @author upw023
 * @version
 * @since JDK 1.6
 * @see
 */
@Controller
public class TestJsonController {
	@RequestMapping(value = "/SimpleData", method = RequestMethod.POST)
	@ResponseBody
	public void SimpleData(String foo, String bar) {
		// return Json("SimpleData", JsonRequestBehavior.AllowGet);
	}

	@RequestMapping(value = "/jsonhome.do", method = RequestMethod.GET)
	public ModelAndView jsonhome(HttpServletRequest request) {

		// String machineIP = RequestJsonUtils.getMachineIP(requestJson);
		// String appTag = RequestJsonUtils.getAppTag(requestJson);
		// JSONObject requestInfo =
		// RequestJsonUtils.getRequestInfo(requestJson);

		return new ModelAndView("json");
	}

	@RequestMapping(value = "/json.do", method = RequestMethod.POST)
	@ResponseBody
	public ModelAndView login(@RequestBody JSONObject requestJson, HttpServletRequest request) {

		// String machineIP = RequestJsonUtils.getMachineIP(requestJson);
		// String appTag = RequestJsonUtils.getAppTag(requestJson);
		// JSONObject requestInfo =
		// RequestJsonUtils.getRequestInfo(requestJson);
		System.out.println(requestJson.toString());
		//JSONArray json = JSONArray.fromObject(requestJson);
		JSONArray json=requestJson.getJSONArray("TestForm");
		List<TestForm> data = (List<TestForm>) JSONArray.toCollection(json, TestForm.class);
		for (Iterator<TestForm> em = data.iterator(); em.hasNext();) {
			TestForm form = em.next();
			System.out.println(form.getFirstName().toString());
			System.out.println(form.getLastName().toString());
			//System.out.println(form.getFirstName());
			//System.out.println(form.getLastName());

		}
		return new ModelAndView("home");
	}

	@RequestMapping(value = "/json2.do", method = RequestMethod.POST)
	@ResponseBody
	public void Employees(@RequestBody List<TestForm> data) {
		for (int i = 0; i < data.size(); i++) {

			TestForm form = data.get(i);
			//System.out.println(form.getFirstName());

		}
		// for (Iterator<TestForm> em = Employees.iterator(); em.hasNext();) {
		// TestForm form = em.next();
		// System.out.println(form.getFirstName());
		// System.out.println(form.getLastName());
		//
		// }
		// return Json("Employees", JsonRequestBehavior.AllowGet);
	}
}
